package com.gitee.zhanyao.schedule.service;

import com.gitee.zhanyao.schedule.job.CountAdd;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class TestService {

    @Autowired
    private Scheduler scheduler;

    public void startJob() throws SchedulerException {
        // cron 表达式，每间隔一分钟
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0 */1 * * * ?");
        //任务名称
        String name = UUID.randomUUID().toString();
        //任务所属分组
        String group = CountAdd.class.getName();
        //创建任务
        JobDetail jobDetail = JobBuilder.newJob(CountAdd.class).withIdentity(name, group).build();
        //创建任务触发器
        Trigger trigger = TriggerBuilder.newTrigger().withIdentity(name, group).withSchedule(scheduleBuilder).build();
        //将触发器与任务绑定到调度器内
        scheduler.scheduleJob(jobDetail, trigger);
    }

}
